package com.llb666.e.shop.api.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.llb666.e.shop.api.dto.SubmitOrderDto;
import com.llb666.e.shop.application.OrderApplication;
import com.llb666.e.shop.domain.entity.OrderStatus;
import com.llb666.e.shop.domain.service.OrderService;
import com.llb666.e.shop.infracore.config.RedisOperator;
import com.llb666.e.shop.infracore.enums.OrderStatusEnum;
import com.llb666.e.shop.infracore.web.BaseResponse;
import com.llb666.e.shop.infracore.web.DataResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * @author 斌~
 * @version 1.0
 * @date 2020/9/29 12:57 上午
 */
@Tag(name = "订单相关接口", description = "订单相关Api接口")
@RestController
@RequestMapping("orders")
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrderController extends BaseController {

    private final OrderService orderService;
    private final RedisOperator redisOperator;
    private final OrderApplication orderApplication;


    /**
     * 通过获取token的方式实现下单接口的幂等性,在下单接口进行校验.
     *
     * @param session 防止一次会话下单的幂等性，防止一个用户在一个浏览器重复下单，600秒 = 10分钟
     * @return JSONResult
     */
    @Operation(summary = "获取订单Token", description = "获取订单Token", method = "POST")
    @PostMapping("/getOrderToken")
    public BaseResponse getOrderToken(HttpSession session) {
        String token = UUID.randomUUID().toString();
        redisOperator.set("ORDER_TOKEN_" + session.getId(), token, 600);
        return DataResponse.ok(token);
    }

    /**
     *  fixme:下单分布式事务如何控制？
     */
    @Operation(summary = "用户下单", description = "用户下单", method = "POST")
    @PostMapping("/create")
    public BaseResponse create(@RequestBody SubmitOrderDto submitOrderDto,
                               HttpServletRequest request,
                               HttpServletResponse response) throws JsonProcessingException {
        return DataResponse.ok(orderApplication.create(submitOrderDto,request,response));
    }

    @PostMapping("notifyMerchantOrderPaid")
    @Operation(summary = "内部接口修改订单状态", description = "内部接口修改订单状态", method = "POST")
    public Integer notifyMerchantOrderPaid(String merchantOrderId) {
        orderService.updateOrderStatus(merchantOrderId, OrderStatusEnum.WAIT_DELIVER.type);
        return HttpStatus.OK.value();
    }

    @Operation(summary = "内部接口获取订单信息", description = "内部接口获取订单信息", method = "POST")
    @PostMapping("getPaidOrderInfo")
    public BaseResponse getPaidOrderInfo(String orderId) {
        OrderStatus orderStatus = orderService.queryOrderStatusInfo(orderId);
        return DataResponse.ok(orderStatus);
    }
}
